package com.jsh.service.impl;

import com.jsh.api.entity.StockAddDTO;
import com.jsh.api.service.StockApiService;
import com.jsh.entity.GoodsAddDTO;
import com.jsh.entity.ShopGoods;
import com.jsh.mapper.ShopGoodsMapper;
import com.jsh.service.IShopGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jsh.uitls.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jsh
 * @since 2022-11-20
 */
@Service
@Slf4j
public class ShopGoodsServiceImpl extends ServiceImpl<ShopGoodsMapper, ShopGoods> implements IShopGoodsService {

    @Reference(mock = "com.jsh.service.impl.MockStockApiServiceImpl",cluster = "fail-fast")
    StockApiService stockApiService;

   public CommonResult addGoods(GoodsAddDTO goodsAddDTO){
       ShopGoods shopGoods = new ShopGoods();
       BeanUtils.copyProperties(goodsAddDTO,shopGoods);

       log.info("GoodsAddDTO的参数为：{}",goodsAddDTO.toString());
       this.baseMapper.insert(shopGoods);
       log.info("ShopGoods的参数为:{}",shopGoods.toString());
       StockAddDTO stockAddDTO = new StockAddDTO();
       stockAddDTO.setGoodsId(shopGoods.getGoodsId());
       stockAddDTO.setAccount(goodsAddDTO.getAccount());
       log.info("stockAddDTO的参数为：{}",stockAddDTO.toString());
       this.stockApiService.addStockAddDTO( stockAddDTO);

       return CommonResult.ok();
   }


    public CommonResult<GoodsAddDTO> getShopGoods(Integer goodsId) {

        GoodsAddDTO goodsAddDTO = new GoodsAddDTO();

        log.info("goodsId的参数为：{}",goodsId);

        ShopGoods shopGoods = this.baseMapper.selectById(goodsId);
        log.info("shopGoods的参数为{}",shopGoods);

        BeanUtils.copyProperties(shopGoods,goodsAddDTO);

        log.info("goodsVO的参数为{}",goodsAddDTO);
        Integer account1 = this.stockApiService.getAccount(goodsId);
        goodsAddDTO.setAccount(account1);
        return CommonResult.ok(goodsAddDTO);

    }
}
